{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7220e68a",
   "metadata": {},
   "source": [
    "# 标识符（identifier）\n",
    "在计算机编程语言中，标识符是用户编程时使用的名字，用于给变量、常量、函数、语句块等命名，以建立起名称和使用之间的关系。\n",
    "标识符有以下要求：\n",
    "1. 第一个字符必须是字母或下划线；\n",
    "2. 标识符的其他部分由字母、数字和下划线组成；\n",
    "3. 标识符对大小写敏感；\n",
    "4. 在python3中，可以使用中文作为变量名，非SACII码字符也是允许的了。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b213ad4",
   "metadata": {},
   "source": [
    "# python保留字\n",
    "保留字，即关键字，我们不能把它作为标识符。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "b8e5fc13",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['False',\n",
       " 'None',\n",
       " 'True',\n",
       " '__peg_parser__',\n",
       " 'and',\n",
       " 'as',\n",
       " 'assert',\n",
       " 'async',\n",
       " 'await',\n",
       " 'break',\n",
       " 'class',\n",
       " 'continue',\n",
       " 'def',\n",
       " 'del',\n",
       " 'elif',\n",
       " 'else',\n",
       " 'except',\n",
       " 'finally',\n",
       " 'for',\n",
       " 'from',\n",
       " 'global',\n",
       " 'if',\n",
       " 'import',\n",
       " 'in',\n",
       " 'is',\n",
       " 'lambda',\n",
       " 'nonlocal',\n",
       " 'not',\n",
       " 'or',\n",
       " 'pass',\n",
       " 'raise',\n",
       " 'return',\n",
       " 'try',\n",
       " 'while',\n",
       " 'with',\n",
       " 'yield']"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import keyword\n",
    "keyword.kwlist"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "31f723f0",
   "metadata": {},
   "source": [
    "# 注释\n",
    "python的单行注释以#开头，多行注释可以用'''或\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "75a218d1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello, world!\n",
      "你好，世界！\n",
      "圣诞节快乐！\n"
     ]
    }
   ],
   "source": [
    "# 这是单行的注释\n",
    "print(\"hello, world!\")\n",
    "print(\"你好，世界！\")\n",
    "print(\"圣诞节快乐！\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "0f76ba1c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Welcome to Python!\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'\\n这里是注释\\n...\\n'"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''\n",
    "多行注释：\n",
    "1\n",
    "2\n",
    "3\n",
    "4\n",
    "'''\n",
    "\"\"\"\n",
    "多行注释：\n",
    "a\n",
    "b\n",
    "c\n",
    "\"\"\"\n",
    "print(\"Welcome to Python!\")\n",
    "\"\"\"\n",
    "这里是注释\n",
    "...\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "761a585c",
   "metadata": {},
   "source": [
    "# 行与缩进\n",
    "python使用缩进表示代码块，不用大括号{}。\n",
    "python严格遵守缩进规则，统一代码块必须包含相同的空格数目"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "f64e69c0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "if True:# if 代码块\n",
    "    print(\"True\")\n",
    "else: # else代码块\n",
    "    print(\"False\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "955ed045",
   "metadata": {},
   "outputs": [
    {
     "ename": "IndentationError",
     "evalue": "unindent does not match any outer indentation level (<tokenize>, line 6)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<tokenize>\"\u001b[1;36m, line \u001b[1;32m6\u001b[0m\n\u001b[1;33m    print(\"Go!!\")# 缩进不一致，运行出错\u001b[0m\n\u001b[1;37m    ^\u001b[0m\n\u001b[1;31mIndentationError\u001b[0m\u001b[1;31m:\u001b[0m unindent does not match any outer indentation level\n"
     ]
    }
   ],
   "source": [
    "if True:# if 代码块\n",
    "    print(\"True\")\n",
    "else: # else代码块\n",
    "    print(\"False\")\n",
    "    print(\"Oh! My God!\")\n",
    " print(\"Go!!\")# 缩进不一致，运行出错"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5c7cc3a3",
   "metadata": {},
   "source": [
    "# 多行语句\n",
    "遇到语句太长，如果都写在一行，不方便阅读代码，我们可以使用反斜杠\\实现换行，将一行语句变成多行语句。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0562a22c",
   "metadata": {},
   "outputs": [],
   "source": [
    "total = 1 + \\\n",
    "        2 + \\\n",
    "        3 + \\\n",
    "        4 + \\\n",
    "        5\n",
    "print(\"total is : \", total)\n",
    "total = 6+\\\n",
    "        7+\\\n",
    "        8+\\\n",
    "        9\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "768d281c",
   "metadata": {},
   "source": [
    "在列表[]，元组()，字典{}等数据结构里的多行语句，不需要反斜杠。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2bc40db2",
   "metadata": {},
   "outputs": [],
   "source": [
    "total = ['one', 'two', 'three',\n",
    "        'four', 'five']\n",
    "print(total)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35c5c750",
   "metadata": {},
   "source": [
    "# 数字(number)类型的变量\n",
    "python有四种数字类型的变量：整型、布尔型、浮点型和复数。\n",
    "1. int整型(integer)， 如 123,879，是一种整数类型；\n",
    "2. bool布尔型，取值只有True和False；\n",
    "3. float浮点型，如 3.1415926;\n",
    "4. complex复数，如 1+2j"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ce1d314",
   "metadata": {},
   "source": [
    "# 字符串string\n",
    "python中使用单引号和双引号作用一样，表示变量类型是字符串。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ca40a095",
   "metadata": {},
   "outputs": [],
   "source": [
    "word = '字符串'\n",
    "sentence = \"我是一句话！\"\n",
    "paragraph = \"\"\"我\\n是一段话，\n",
    "是一个多行字符串。\n",
    "\"\"\"\n",
    "print(word)\n",
    "print('-----------')\n",
    "print(sentence)\n",
    "print('-----------')\n",
    "print(paragraph)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c8c9ddb8",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(len(word)) # len()函数，用于获取字符串的长度，是字符串自带的库函数\n",
    "print(len(sentence))\n",
    "print(len(paragraph)) # 17，15个中文字符，2行，每行有一个换行符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f290debb",
   "metadata": {},
   "outputs": [],
   "source": [
    "greetings = \"hello, NaNa!\"\n",
    "print(len(greetings))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "c6084b5c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Well done!Tom\n"
     ]
    }
   ],
   "source": [
    "# 字符串拼接，用+就可以\n",
    "print(\"Well done!\"+\"Tom\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "56e1c3d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字符串索引，从左往右是以0开始，从右往左是从-1开始\n",
    "str_test = \"abcde\"\n",
    "print(str_test[0])\n",
    "print(str_test[1])\n",
    "print(str_test[2])\n",
    "print(str_test[3])\n",
    "print(str_test[4])\n",
    "print('---------------')\n",
    "print(str_test[-1])\n",
    "print(str_test[-2])\n",
    "print(str_test[-3])\n",
    "print(str_test[-4])\n",
    "# 不要超过可以索引的范围\n",
    "print(str_test[1000])\n",
    "print(str_test[-900])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "deb90618",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 截取字符串，获得子串，称为切片操作\n",
    "# 语法格式如下：变量名[start;end;step]\n",
    "# 注意截取的字符串不包含end\n",
    "# 字符串    'abcdefg'\n",
    "# 对应的索引 0123456\n",
    "str_test = 'abcdefg'\n",
    "print(\"raw string: \", str_test)\n",
    "print(str_test[0:3:1])# 从索引为0开始，步长为1\n",
    "print(str_test[:3])# 起始索引默认为0的话，可以省略不写；步长为1的话，也可以省略不写\n",
    "print(str_test[1:]) # 终止索引默认是子串截取到最后一个字符\n",
    "print(str_test[::2])# 从开始到结束，默认每两个字符取一个字符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1ee02de2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
